import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from "@/api/index";

const state = {
    token: getToken(),
    userInfo: {}
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    USER_INFO: (state, userInfo) => {
        state.userInfo = userInfo
    }
}

const actions = {
    // user login
    login({ commit }, userInfo) {
        const { mobile, verify_code } = userInfo
        return new Promise((resolve, reject) => {
            login(userInfo).then(res => {
                const { data } = res
                setToken(data.token)
                commit('SET_TOKEN', data.token)
                commit('USER_INFO', data.user_info)
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}